<?php

$groups = R::findAll('group');
$g = array();
foreach($groups as $group){

    $history = [];
    $hist = [];

    foreach($group->ownGactivityList as $activity){
        if($activity->date_from !== $activity->date_to){
            $history[] = "User " . R::load('user', $activity->user_id)->email .
                         " was active from " . $activity->date_from .
                        " to " .$activity->date_to;
            $hist[$activity->user_id] = $activity->group_id;
        }else { //if (isset($hist[$activity->user_id]) && $hist[$activity->user_id] == $activity->group_id) {
            $history[] = "User " . R::load('user', $activity->user_id)->email .
                        " is active from " . $activity->date_from;
        }
    }

    $active_developers = [];
    foreach ($group->sharedUserList as $u) {
        if(in_array($u->id, json_decode($group->developers)))
            $active_developers[] = $u->id;
    }



    $g[] = [
        'id'            => $group->id,
        'developers'    => $active_developers,
        'kanban_master' => $group->kanban_master,
        'product_owner' => $group->product_owner,
        'name'          => $group->name,
        'history'       => $history,
    ];

}
$groups = $g;

if(!isset($getResultAsArray) || !$getResultAsArray){
    jsonSuccessDie(json_encode($g), '');
}